package quan.dong.common;

import java.nio.ByteBuffer;

import quan.dong.pricealert.R;
import quan.dong.pricealert.SplashScreen;
import android.R.bool;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Common {
	public static MyLogger myLogger = MyLogger.getInstance("");
	private static final String TAG = Common.class.getSimpleName();

	public static float convertStringToFloat(String input){
		try{
			return Float.parseFloat(input);						
		}catch(Exception e){
			return -1;
		}
	}
	public static int byteArrayToInt(byte[] b) {
		return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16
				| (b[0] & 0xFF) << 24;
	}

	public static byte[] intToByteArray(int a) {
		return new byte[] { (byte) ((a >> 24) & 0xFF),
				(byte) ((a >> 16) & 0xFF), (byte) ((a >> 8) & 0xFF),
				(byte) (a & 0xFF) };
	}

	public static int ConvertByteArrayToInt(byte[] bytes, int offset) {
		int ret = 0;
		for (int i = 0; i < 4 && i + offset < bytes.length; i++) {
			ret <<= 8;
			ret |= (int) bytes[i] & 0xFF;
		}
		return ret;
	}

	public static void ConvertIntToByteArray(int value, byte[] bytes, int offset) {
		for (int i = 0; i < 4; i++) {
			bytes[offset + i] = (byte) (value >>> (i * 8));
		}
	}
	public static void ConvertFloatToByteArray(Float value, byte[] bytes,
			int offset) {
		byte[] valueArray = ByteBuffer.allocate(4).putFloat(value).array();
		for (int i = 0; i < valueArray.length; i++) {
			bytes[offset + i] = valueArray[i];
		}
	}

	public static void ConvertStringToByteArray(String value, byte[] bytes,
			int offset) {
		byte[] valueArray = value.getBytes();
		for (int i = 0; i < valueArray.length; i++) {
			bytes[offset + i] = valueArray[i];
		}
	}
	
	public static float ConvertByteArrayToFloat(byte[] bytes, int offset) {
		byte[] floatValue = new byte[4];
		for (int i = 0; i < 4 ; i++) {
			floatValue[i]=bytes[offset+i];
		}		
		return ByteBuffer.wrap(floatValue).getFloat();
	}

	public static String GetPhoneNumber(Context context) {
		String number = null;
		try {
			String service = Context.TELEPHONY_SERVICE;
			TelephonyManager tel_manager = (TelephonyManager) context
					.getSystemService(service);
			number = tel_manager.getLine1Number();
		} catch (Exception e) {
			return "";
		}
		return number;
	}

	public static String GetImeiNumber(Context context) {
		String number = null;
		try {
			TelephonyManager tManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			number = tManager.getDeviceId();
		} catch (Exception e) {
			return "";
		}
		return number;
	}

	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager connectivity = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			if (connectivity == null) {
				return false;
			} else {
				NetworkInfo[] info = connectivity.getAllNetworkInfo();
				if (info != null) {
					for (int i = 0; i < info.length; i++) {
						if (info[i].getState() == NetworkInfo.State.CONNECTED) {
							return true;
						}
					}
				}
			}
		} catch (Exception e) {
			Common.myLogger.error(Log.getStackTraceString(e), TAG);
		}
		return false;
	}
	public static void showNofitication(Context context,String tickerText,String title,String text,Boolean isAlarm){
		try{
			NotificationManager notificationManager= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
	        Notification notification= new Notification(R.drawable.ic_launcher, tickerText, System.currentTimeMillis());
	        Intent intent= new Intent(context, SplashScreen.class);
	        intent.putExtra("extendedText", text);
	        intent.putExtra("extendedTitle", title);
	        PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
	        notification.setLatestEventInfo(context, title, text, launchIntent);
	        int notificationId= 1;
	        notificationManager.notify(notificationId, notification);
	        if(isAlarm){
	        	playAlarmSound(context);
	        }
		}catch(Exception e){
			
		}
	}
	public static void playAlarmSound(Context context){
		try {
		    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		    Ringtone r = RingtoneManager.getRingtone(context, notification);
		    r.play();
		} catch (Exception e) {
		    e.printStackTrace();
		}
	}
}
